package io.github.eiot.json.deserializer;

import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import io.github.eiot.codec.BCDTime;

import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * created by wang007 on 2025/9/27
 */
public class BCDTimeDeserializer extends JsonDeserializer<BCDTime> {

    public static final BCDTimeDeserializer INSTANCE = new BCDTimeDeserializer();

    private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    @Override
    public BCDTime deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JacksonException {
        String text = p.getText();
        LocalDateTime dateTime = LocalDateTime.parse(text, FORMATTER);
        return BCDTime.from(dateTime);
    }
}
